#include "worldsimulator.h"
#include "polygonbody.h"
#include "edgebody.h"
#include "wind.h"
#include "stone.h"

WorldSimulator::WorldSimulator()
{
    b2Vec2 gravity(0.f, 0.f);
    world = new b2World(gravity);
}

void WorldSimulator::step()
{
    float timeStep = 1.f / 100.f;
    int velocityIterations = 6;
    int positionIterations = 2;

    BodyWrapper *object;
    Stone *stone;
    for(std::vector<BodyWrapper*>::iterator iter = objects.begin(); iter != objects.end();) {
        object = *iter;
        stone = dynamic_cast<Stone*>(object);
        if(stone && !stone->inRegion(-15, -20, 20, 15)) {
            delete stone;
            iter = objects.erase(iter);
        } else {
            object->step();
            ++ iter;
        }
    }

    world->Step(timeStep, velocityIterations, positionIterations);
}

WorldSimulator *WorldSimulator::ins = 0;
